"use strict"

const uuid = require("uuid")
const BaseController = require("./base")
class RoleController extends BaseController {
  async Success(result, msg) {
    const { ctx } = this
    ctx.body = {
      code: 200,
      data: {
        code: 200,
        msg: msg,
        data: result,
      },
    }
  }
  async Error(msg) {
    const { ctx } = this
    ctx.body = {
      code: 5000,
      data: {
        code: 5000,
        msg,
      },
    }
  }
  //获取所有角色信息
  async index() {
    const { ctx, service } = this
    const { current, size, ...where } = ctx.query
    const result = await service.role.list(
      isNaN(current) ? 1 : parseInt(current),
      isNaN(size) ? 3 : parseInt(size),
      where
    )
    if (result) {
      this.Success(result, "获取成功")
    } else {
      this.Error("获取失败")
    }
  }
  //获取单条角色信息
  async single() {
    const { ctx, service } = this
    const id = ctx.params.id
    const result = await service.role.single(id)
    if (result) {
      this.Success(result, "获取成功")
    } else {
      this.Error("获取失败")
    }
  }
  //创建角色
  async create() {
    const { ctx, service } = this
    const role = ctx.request.body
    role.roleId = uuid.v4()
    role.createTime = Date.now()
    const result = await service.role.create(role)
    if (result.affectedRows === 1) {
      this.Success(result, "新增成功")
    } else {
      this.Error("新增失败")
    }
  }
  //修改角色
  async update() {
    const { ctx, service } = this
    const id = ctx.params.id
    const role = ctx.request.body
    role.id = id
    role.updateTime = Date.now()
    const result = await service.role.update(role)
    if (result.affectedRows === 1) {
      this.Success(result, "修改成功")
    } else {
      this.Error("修改失败")
    }
  }

  //删除角色
  async destroy() {
    const { ctx, service } = this
    const id = ctx.params.id
    //console.log(id)
    const result = await service.role.destroy(id)
    if (result.affectedRows === 1) {
      this.Success(result, "删除成功")
    } else {
      this.Error("删除失败")
    }
  }
  async getRes() {
    const { ctx, service } = this
    const result = await service.role.getResouce()
    if (result) {
      this.Success(result, "获取成功")
    } else {
      this.Error("获取失败")
    }
  }
  async setRes() {
    const { app, ctx, service } = this
    let body = ctx.request.body
    console.log(body, "134行")
    const result = await service.role.setRes(body)
    if (result) {
      this.Success(result, "设置成功")
    } else {
      this.Error("设置失败")
    }
  }
  async getUser() {
    const { app, ctx, service } = this
    const result = await service.role.getUser()
    if (result) {
      this.Success(result, "获取成功")
    } else {
      this.Error("获取失败")
    }
  }
  async setUser() {
    const { app, ctx, service } = this
    let body = ctx.request.body
    console.log(body)
    const result = await service.role.setUser(body)
    if (result) {
      this.Success(result, "获取成功")
    } else {
      this.Error("获取失败")
    }
  }
}

module.exports = RoleController
